{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d205dfcd",
   "metadata": {},
   "source": [
    "# ----\n",
    "*记录人（学习人）：蒋琪\n",
    "*时间：2021-11-15\n",
    "*week12\n",
    "*学习线上资源"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "54aae7e7",
   "metadata": {},
   "source": [
    "##本周知识点\n",
    "###1.字典的一般方法\n",
    ">1.字典的查询 查询key->value\n",
    ">>1.先看字典的key是否存在 : in\n",
    ">2.字典的修改\n",
    ">3.字典的删除（dict_name[key] = value）\n",
    "\n",
    "###2.字典的进阶方法\n",
    "\n",
    ">1.字典的初始化\n",
    ">>1.D.fromkeys()\n",
    ">>2.D.setdefault()\n",
    "\n",
    "###3.字典的实践（1）课本\n",
    "###4.字典的实践（2)课本\n",
    "###5.字典的实践（3）课外"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "70473e60",
   "metadata": {},
   "source": [
    "###1.字典的一般方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "79751b31",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-4-a48b6ab4d083>, line 3)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-4-a48b6ab4d083>\"\u001b[1;36m, line \u001b[1;32m3\u001b[0m\n\u001b[1;33m    \"stu1802\":\"xiaofang\"\u001b[0m\n\u001b[1;37m             ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "stu_info = {\n",
    "    \"stu1801\":\"zhichao\"\n",
    "    \"stu1802\":\"xiaofang\"\n",
    "    \"stu1803\"::\"lihua\"\n",
    "}\n",
    "print(stu_info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "090cf646",
   "metadata": {},
   "outputs": [],
   "source": [
    ">1.字典的查询\n",
    ">>1.直接查找 dict_name[key]当关键key不存在时，报错，keyerror:'stu1804'\n",
    ">>2.get()的方法3.get(k[,d])当关键词key不存在时，返回none不存在，不会报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3b995881",
   "metadata": {},
   "outputs": [],
   "source": [
    "#直接查找 dict_name[key]\n",
    "stu_info[\"stu1801\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e4eb8270",
   "metadata": {},
   "outputs": [],
   "source": [
    "#使用get方法查找\n",
    "help(stu_info.get)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5fc17699",
   "metadata": {},
   "outputs": [],
   "source": [
    "stu_info.get(\"stu1801\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ae786cc0",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(stu_info.get(\"stu1804\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c107249e",
   "metadata": {},
   "outputs": [],
   "source": [
    ">2.修改&新增\n",
    ">>1.先查询 查看key是否存在 使用in 查询成员"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cbccafa0",
   "metadata": {},
   "outputs": [],
   "source": [
    "#查询 stu1804\n",
    "\"stu1804\" in stu_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "93cd9d90",
   "metadata": {},
   "outputs": [],
   "source": [
    "#查询1803\n",
    "\"stu1803\" in stu_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b1aca942",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 如果key存在 修改 \"stu1803\":\"lihua\" -->\"stu1803\":\"liuyu\"\n",
    "if \"stu_1803\" in stu_info:\n",
    "    stu_info[\"stu1803\"] = \"liuyu\"\n",
    "stu_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eaa7551b",
   "metadata": {},
   "outputs": [],
   "source": [
    ">3.字典的删除\n",
    ">>1.D.pop()的一般操作如下：\n",
    ">>>1.先查询是否存在 in\n",
    ">>>2.再使用删除的方法\n",
    ">>2.D,popitem()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6e51084f",
   "metadata": {},
   "outputs": [],
   "source": [
    "help(stu_info.po)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "282adb5e",
   "metadata": {},
   "outputs": [],
   "source": [
    "stu_info.pop( ) # 不正确，不同于列表（删除最后一个）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9470ba8c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 字典删除的正确使用\n",
    "if 'stu1803' in stu_info:\n",
    "stu_info.pop('stu1803')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d00f30b3",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'stu_info' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-3-9c106b08b1bc>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mstu_info\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'stu_info' is not defined"
     ]
    }
   ],
   "source": [
    "stu_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "132e69a6",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'stu_info' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-6-e54377a907f4>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# 如果不限查询key是否存在，有可能报错：keyerror：\"1804\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mstu_info\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'stu1804'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'stu_info' is not defined"
     ]
    }
   ],
   "source": [
    "# 如果不限查询key是否存在，有可能报错：keyerror：\"1804\"\n",
    "stu_info.pop('stu1804')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "6c7d518c",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'stu_info' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-5-7a6fce7b4c81>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mhelp\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mstu_info\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpopitem\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'stu_info' is not defined"
     ]
    }
   ],
   "source": [
    "help(stu_info.popitem)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "317a7479",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 目标:查询元音字母是否出现在word文本当中，并且统计出现的次数\n",
    "vomels = [\"a\",\"e\",\"i\",\"o\",\"u\"]\n",
    "word = input(\"provibe a word to search for vowels:\")\n",
    "found = {}\n",
    "\n",
    "#字典 初始化 的指\n",
    "# found[\"a\"]=0\n",
    "# found[\"e\"]=0\n",
    "\n",
    "for i in vowels:\n",
    "    #   使用字典的新增  构建初始化字典\n",
    "    found[i] = 0\n",
    "\n",
    "# 循环用户输入的文本 ， 出现一次元音字符 ，对应的元音字符+1\n",
    "for i in word:\n",
    "   if i in vowels:\n",
    "        found[i] +=1\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6b306d64",
   "metadata": {},
   "outputs": [],
   "source": [
    ">字典的进阶方法：初始化所有值\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9a7f07e1",
   "metadata": {},
   "outputs": [],
   "source": [
    "help(found.fromkeys)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "46ad8805",
   "metadata": {},
   "outputs": [],
   "source": [
    "vowels = [\"a\",\"e\",\"i\",\"o\",\"u\"]\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "found = {}\n",
    "\n",
    "#初始化 字典\n",
    "found = found.fromkeys(vowels,0)\n",
    "\n",
    "for i in word:\n",
    "    if i in vowels:\n",
    "        found[i] +=1\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2dcf5264",
   "metadata": {},
   "outputs": [],
   "source": [
    ">字典的进阶方法：初始化特定的值 setdefault()\n",
    "\n",
    ">>*查询key是否出现在字典中\n",
    ">>>1.出现过，就当没看见，不做任何操作\n",
    ">>>2.key未出现过，给定key及value（特定初始化的值）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8dd0a1ef",
   "metadata": {},
   "outputs": [],
   "source": [
    "help(found.setdefault)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "37036ee4",
   "metadata": {},
   "outputs": [],
   "source": [
    "stu_password = {\n",
    "    \"xiaoming\":\"22223333\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "04821862",
   "metadata": {},
   "outputs": [],
   "source": [
    "stu_password.setdefault(\"lihua\",\"12345\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5e1b6ace",
   "metadata": {},
   "outputs": [],
   "source": [
    "stu_password.setdefault(\"xiaoming\",\"123456\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6f7ff739",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 优化 ： 实践（1）\n",
    "\n",
    "vowels = [\"a\",\"e\",\"i\",\"o\",\"u\"]\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "found = {}\n",
    "\n",
    "for i in word:\n",
    "    if i in vowels:\n",
    "        found.setdefault(i,0) # 特定情况下（字典中不存在该元素时，初始化其值）\n",
    "        found[i] +=1\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "13db5740",
   "metadata": {},
   "outputs": [],
   "source": [
    "{'e':1,'o':2,'i',:1'a':1}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "af4841d3",
   "metadata": {},
   "outputs": [],
   "source": [
    "总结字典的所有方法，写出一个表格"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
